<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class User extends AppModel {

    public $name = 'User';
    //var $primaryKey = 'email';
    var $validate = array(
        'password' => array(
            'must enter password' => array(
                'rule' => 'notEmpty',
                'message' => 'please enter password',
            ),
            'minimum length 8' => array('rule' => array('minLength', 8),
                'message' => 'password must be at least 8 characters long.'),
            'matching Passwords' => array('rule' => 'matchPasswords',
                'message' => 'Your passwords do not match'
            )
        ),
        'first_name' => array(
            'must enter first name' => array('rule' => 'notEmpty',
                'message' => 'please enter the full mame'
            )
        ),
        'last_name' => array(
            'must enter last name' => array('rule' => 'notEmpty',
                'message' => 'please enter the full mame'
            )
        ),
        'confirm password' => array(
            'must enter pw validation' => array('rule' => 'notEmpty',
                'message' => 'please enter password confirmation'
        )),
        'email' => array(
            'must enter email' => array('rule' => 'notEmpty',
                'message' => 'please enter the E-mail'
            ),
            'must be uniqe' => array('rule' => 'isUnique',
                'message' => 'a user with this E-mail already exists'),
            'must be valid email' => array('rule' => 'email',
                'message' => 'Please Enter a Valid E-mail')
        )
    );

    public function beforeSave() {
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }

    public function matchPasswords($data) {
        if ($data['password'] == $this->data['User']['confirm password']) {
            return true;
        }
        $this->invalidate('confirm password', 'Your passwords do not match');
        return false;
    }

    function getPermissionLevel($accID, $email) {
        //select the permission level of the account and user id from table accounts_has_users
        $permission = $this->query("Select permission from accounts_has_users where accounts_has_users.user_email = " . "'" . $email . "'" . " AND accounts_has_users.account_id = " . $accID);
        // if the account and user id combination does not exist return 0 
        if ($permission == array()) {
            return 0;
        } else {
            return $permission[0]['accounts_has_users']['permission'];
        }
    }

    /**
     * Gets the permission of a user in a specified account
     * param int $accountID
     * param String $UserEmail
     * return String $permissionLevel
     */
    public function getPermission($accountID, $UserEmail) {
        $permission = $this->query("Select permission from accounts_users where accounts_users.account_id =" . $accountID . " AND accounts_users.user_email ='$UserEmail'");
        $permissionLevel = $permission[0]['accounts_users']['permission'];
        return $permissionLevel;
    }
    public function getFullName($email){
        $current_user = $_SESSION['Auth']['User']['email'];
        if($current_user == $email){
            return "Me" ;
        }
        else {
            $fullName = $this->find('all', array('fields'=> array('first_name', 'last_name'),'conditions' => array('email'=>$email)));
            $result = $fullName[0]['User']['first_name'].' '.$fullName[0]['User']['last_name'];
            return $result;
        }
    }

    /**
     * Description of "getModeratorsByName" 
     * 
     * this function returns an array of all the moderators of a given accID
     *
     * @param $accID
     * @return array 
     *
     * @author karim kamel
     */
    function getModeratorsByName($accID) {
        return $this->query("SELECT users.email
FROM accounts_users, users
WHERE users.email = accounts_users.user_email
AND accounts_users.account_id = " . $accID . " AND (accounts_users.permission = 'moderator')");
    }

    /**
     * Description of "getEditorsByName"
     *
     * this function returns an array of all the editorss of a given accID
     *
     * @param $accID
     * @return array 
     *
     * @author karim kamel
     */
    function getEditorsByName($accID) {
        return $this->query("SELECT users.email
FROM accounts_users, users
WHERE users.email = accounts_users.user_email
AND accounts_users.account_id = " . $accID . " AND (accounts_users.permission = 'editor')");
    }

}

?>
